package com.example.devices

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.example.base.app.path.RouterPath
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.databinding.ActivityDevicesBinding
import com.example.devices.adapter.DevicesAdapter
import com.example.devices.vm.DevicesViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/devices/DevicesActivity")
@AndroidEntryPoint
class DevicesActivity : BaseMvvmActivity<ActivityDevicesBinding>() {
    val devicesAdapter = DevicesAdapter()
    private val devicesViewModel:DevicesViewModel by viewModels()
    override fun initView() {
        mDataBinding.rvDevices.layoutManager = LinearLayoutManager(this)
        mDataBinding.rvDevices.adapter = devicesAdapter
        mDataBinding.more.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_DEVICES_POST).navigation()
        }
        mDataBinding.back.setOnClickListener {
            finish()
        }
    }

    override fun loadData() {
        devicesViewModel.loadDevicesData()
    }

    override fun observeDataChange() {
        devicesViewModel.mSuccess.observe(this){
            Log.e("DevicesPage","=======>${GsonUtils.toJson(it)}")
            devicesAdapter.data.addAll(it.reversed())
            devicesAdapter.notifyDataSetChanged()
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_devices
}